﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace RanchBuddy.Core.Services.Impl
{
    public class LoggingService : ILoggingService
    {
        public static void WriteLog(string className, string methodName, string message, LoggingType type, Exception e)
        {
            WriteLog(className, methodName,
                     message + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace, type);
        }
        public static void WriteLog(string className, string methodName, string message, LoggingType type)
        {
            //log everything
            if(ConfigurationService.GetLoggingType() == LoggingType.Verbose)
            {
                WriteToFile(className, methodName, message);
            }

            //log errors only
            if(ConfigurationService.GetLoggingType() == LoggingType.Error)
            {
                WriteToFile(className, methodName, message);
            }
        }

        private static void WriteToFile(string className, string methodName, string message)
        {
            string path = ConfigurationService.GetLogFileDirectory() + DateTime.Now.Year + "\\" + DateTime.Now.Month +
                          "\\";
            string file = DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "_log.txt";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            File.AppendAllText(path + file,DateTime.Now.ToString("u") + " " + className + "." + methodName + "(): " + message);
        }
    }

    public enum LoggingType
    {
        Verbose,
        Error
    }
}
